package com.CmJava.controller;

import com.CmJava.entity.ResultEntity;
import com.CmJava.entity.blog.Blog;
import com.CmJava.entity.blog.BlogModel;
import com.CmJava.entity.blog.BlogRedis;
import com.CmJava.service.BlogService;
import com.CmJava.utils.DownUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class BlogController {


    private int pageNum=6;

    @Autowired
    private BlogService blogService;

    // 获取博客列表
    @RequestMapping("getBlogList")
    @ResponseBody
    public ResultEntity getBlogList(@RequestParam("keyWord") String keyWord,
                                    @RequestParam("currentPage") String currentPage) {
        ResultEntity resultEntity = blogService.getBlogList(keyWord, currentPage, pageNum);
        return resultEntity;
    }

    // 获取近期博客列表
    @RequestMapping("getRecentBlog")
    @ResponseBody
    public ResultEntity getRecentBlog() {
        ResultEntity resultEntity= blogService.getRecentBlog(1,5);
        return resultEntity;
    }

    // 获取新博客的id
    @RequestMapping("getNewBlogId")
    @ResponseBody
    public ResultEntity getNewBlogId() {
        ResultEntity resultEntity= blogService.getNewBlogId();
        return resultEntity;
    }

    // 添加博客到Mysql
    @RequestMapping("adminHtml/addBlogMysql")
    @ResponseBody
    public ResultEntity addBlogMysql(@RequestParam("blogId") String blogId,
                                     @RequestParam("blogTitle") String blogTitle,
                                     @RequestParam("blogImg") String blogImg,
                                     @RequestParam("blogSum") String blogSum) {
        String thisMoment = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        Blog blog = new Blog(Integer.parseInt(blogId), blogTitle, blogSum, blogImg,0, thisMoment);
        ResultEntity resultEntity= blogService.addBlogMysql(blog);
        return resultEntity;
    }

    // 添加博客到Redis
    @RequestMapping("adminHtml/addBlogRedis")
    @ResponseBody
    public ResultEntity addBlogRedis(@RequestParam("blogId") String blogId,
                                     @RequestParam("blogRedisData") String blogRedisData) {
        String key="blog"+blogId+"_for_MyWeb";
        ResultEntity resultEntity= blogService.addBlogRedis(key,blogRedisData);
        return resultEntity;
    }

    // 从mysql获取某个博客
    @RequestMapping("getBlogMysql")
    @ResponseBody
    public ResultEntity getBlogMysql(@RequestParam("blogId") String blogId) {
        ResultEntity resultEntity= blogService.getBlogMysql(blogId);
        return resultEntity;
    }

    // 从redis获取某个博客
    @RequestMapping("getBlogRedis")
    @ResponseBody
    public ResultEntity getBlogRedis(@RequestParam("blogId") String blogId) {
        String key="blog"+blogId+"_for_MyWeb";
        ResultEntity resultEntity= blogService.getBlogRedis(key);
        return resultEntity;
    }

    // 获取当前博客拥有的模块数和子模块数
    @Deprecated
    @RequestMapping("getBlogModelAndChildNum")
    @ResponseBody
    public ResultEntity getBlogModelAndChildNum(@RequestParam("blogId") String blogId) {
        String key="blog"+blogId+"_for_MyWeb";
        ResultEntity resultEntity= blogService.getBlogModelAndChildNum(key);
        return resultEntity;
    }

    // 提交MySQL博客修改
    @Deprecated
    @RequestMapping("adminHtml/updateBlogMysql")
    @ResponseBody
    public ResultEntity updateBlogMysql(@RequestParam("blogId") String blogId,
                                        @RequestParam("blogTitle") String blogTitle,
                                        @RequestParam("blogImg") String blogImg,
                                        @RequestParam("blogSum") String blogSum) {
        int visitNum = ((Blog) blogService.getBlogMysql(blogId).getData()).getVisitNum();

        Blog blog = new Blog(Integer.parseInt(blogId), blogTitle, blogSum, blogImg, visitNum,null);
        ResultEntity resultEntity= blogService.updateBlogMysql(blog);
        return resultEntity;
    }

    // 更新博客某一模块
    @Deprecated
    @RequestMapping("adminHtml/updateThisBlogModel")
    @ResponseBody
    public ResultEntity updateThisBlogModel(@RequestParam("blogId") String blogId,
                                            @RequestParam("modelId") String modelId,
                                            @RequestBody BlogModel blogModel) {
        String key="blog"+blogId+"_for_MyWeb";
        ResultEntity resultEntity= blogService.updateThisBlogModle(key,modelId,blogModel);
        return resultEntity;
    }

    // 增加mysql博客评论的表
    @RequestMapping("adminHtml/addBlogCommentMysql")
    @ResponseBody
    public ResultEntity addBlogCommentMysql(@RequestParam("blogId") String blogId) {
        ResultEntity resultEntity = blogService.addBlogCommentMysql(blogId);
        return resultEntity;
    }

    // 管理员页面获取博客列表
    @RequestMapping("getAdminBlogList")
    @ResponseBody
    public ResultEntity getAdminBlogList() {
        ResultEntity resultEntity = blogService.getAdminBlogList();
        return resultEntity;
    }

    // 博客浏览量+1
    @RequestMapping("blogVisitNumIncrement")
    @ResponseBody
    public ResultEntity blogVisitNumIncrement(@RequestParam("blogId") String blogId) {
        blogService.blogVisitNumIncrement(blogId);
        return ResultEntity.successWithoutMessage();
    }


    //删除模块
    @Deprecated
    @RequestMapping("adminHtml/deleteBlogModel")
    @ResponseBody
    public ResultEntity deleteBlogModel(@RequestParam("blogId") String blogId,@RequestParam("modelIndex") String modelIndex) {
         return blogService.deleteBlogModel(blogId,Integer.parseInt(modelIndex)-1);
    }


    //删除子模块
    @Deprecated
    @RequestMapping("adminHtml/deleteBlogChildModel")
    @ResponseBody
    public ResultEntity deleteBlogChildModel(@RequestParam("blogId") String blogId,@RequestParam("childModelIndex") String childModelIndex) {
        int modelIndex = Integer.parseInt(childModelIndex.split("-")[0]) - 1;
        int childIndex = Integer.parseInt(childModelIndex.split("-")[1]) - 1;

        return blogService.deleteBlogChildModel(blogId,modelIndex,childIndex);
    }

    //备份某篇博客
    @RequestMapping("adminHtml/backupBlog")
    @ResponseBody
    public ResultEntity backupBlog(@RequestParam("blogId") String blogId) {
        return blogService.backupBlog(blogId);
    }

    //下载博客备份
    @RequestMapping("adminHtml/downBackupBlog")
    @ResponseBody
    public ResultEntity downBackupBlog(HttpServletResponse response) {
        File zipFile = (File) blogService.downBackupBlog().getData();
        DownUtil.downFileOutsideJar(response,zipFile);
        return null;
    }
}
